package heven.holt.eyepetizer.utils

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import heven.holt.eyepetizer.databinding.EyeFragmentMainBinding

class EyeMainNavigation(
    private val binding: EyeFragmentMainBinding,
    private val navigationClick: (Int) -> Unit,
    private val navigationDoubleClick: ((Int) -> Unit)? = null
) {
    private val map: Map<ImageView, TextView> = mapOf(
        binding.navigationBar.ivHomePage to binding.navigationBar.tvHomePage,
        binding.navigationBar.ivCommunity to binding.navigationBar.tvCommunity,
        binding.navigationBar.ivNotification to binding.navigationBar.tvNotification,
        binding.navigationBar.ivMine to binding.navigationBar.tvMine
    )

    fun navigation(view: View) {
        if (view.isSelected) {
            navigationDoubleClick(view)
            return
        }

        for ((key, value) in map) {
            key.isSelected = false
            value.isSelected = false
        }

        view.isSelected = true
        map[view]?.isSelected = true
        navigationClick(view)
    }

    private fun navigationClick(view: View) {
        val index = findIndexByView(view)
        navigationClick.invoke(index)
    }

    private fun navigationDoubleClick(view: View) {
        val index = findIndexByView(view)
        navigationDoubleClick?.invoke(index)
    }

    private fun findIndexByView(view: View): Int {
        val index = when (view) {
            binding.navigationBar.ivHomePage -> 0
            binding.navigationBar.ivCommunity -> 1
            binding.navigationBar.ivNotification -> 2
            binding.navigationBar.ivMine -> 3
            else -> 0
        }
        return index
    }
}